<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="68px">
      <el-form-item label="名称" prop="entName">
        <el-input v-model.trim="queryParams.entName" placeholder="请输入名称" clearable style="width: 240px"
          @keyup.enter="handleQuery" @clear="handleQuery" @input="handleInput" />
      </el-form-item>
      <el-form-item label="禁用状态" prop="state" label-width="100px">
        <el-select v-model="queryParams.state" clearable placeholder="请选择禁用状态" @change="handleQuery"  style="width: 240px">
          <el-option v-for="item in options3" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:role:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:role:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['system:role:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>
    <!-- 表格数据 -->
    <el-table v-loading="loading" size="large" :max-height="tableHeight" :data="dataList">
      <el-table-column type="index" label="序号" width="120" align="center">
        <template #default="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="公司全称" v-if="columns[0].visible" prop="entName" align="center" width="250" />
      <el-table-column label="公司简称" v-if="columns[1].visible" prop="entShortName" align="center" width="100" />
      <el-table-column label="公司地址" v-if="columns[2].visible" prop="entAddr" align="center" width="250" />
      <el-table-column label="所属行业" v-if="columns[3].visible" prop="industry" align="center" width="150" />
      <el-table-column label="企业规模" v-if="columns[4].visible" prop="entSize" align="center" width="100" />
      <el-table-column label="企业性质" v-if="columns[5].visible" align="center" width="200">
        <template #default="scope">
        <span>{{ options2.find(item => item.id == scope.row.entType)?.name || '' }}</span>
      </template>
      </el-table-column>
      <el-table-column label="企业类型" v-if="columns[6].visible" align="center" width="100">
        <template #default="scope">
          <span>{{ options1.find(item => item.id == scope.row.entStyle)?.name || '' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品和业务" v-if="columns[7].visible" prop="mainProducts" align="center" width="300" />
      <el-table-column label="禁用状态" v-if="columns[6].visible" align="center" width="100">
        <template #default="scope">
          <span :style="{ color: scope.row.state === 0 ? 'red' : '' }">{{ options3.find(item => item.id == scope.row.state)?.name || '' }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" min-width="160px" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" v-model="open" width="900px" append-to-body :close-on-click-modal="false">
      <el-form ref="roleRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="企业名称" prop="entName">
              <el-input v-model.trim="form.entName" placeholder="请输入企业名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业简称" prop="entShortName">
              <el-input v-model.trim="form.entShortName" placeholder="请输入企业简称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业地址">
              <el-input v-model="form.entAddr" placeholder="请输入企业地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属行业">
              <el-input v-model="form.industry" placeholder="请输入所属行业" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业规模">
              <el-input v-model="form.entSize" placeholder="请输入企业规模" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业性质">
              <el-select v-model="form.entType" placeholder="请选择" style="width: 240px">
                <el-option v-for="item in options2" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业类型">
              <el-select v-model="form.entStyle" placeholder="请选择" style="width: 240px">
                <el-option v-for="item in options1" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品和业务">
              <el-input v-model="form.mainProducts" type="textarea" placeholder="请输入企业主要产品和业务" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业管理状态" label-width="130">
              <el-radio-group v-model="form.state">
                <el-radio :value="1">正常使用</el-radio>
                <el-radio :value="0">已停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { useInfoStore } from '@/store/modules/info.js'
const userInfo = useInfoStore();

const router = useRouter();
const { proxy } = getCurrentInstance();
// const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
const tableHeight = ref(document.documentElement.scrollHeight - 280 + "px");


const dataList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const menuOptions = ref([]);
const deptOptions = ref([]);
const menuRef = ref(null);
const options1 = ref([
  { id: 1, name: '国营' },
  { id: 2, name: '国有控股' },
  { id: 3, name: '私营' },
]);
const options2 = ref([
  { id: 1, name: '有限责任公司' },
  { id: 2, name: '股份有限公司' },
  { id: 3, name: '合伙企业' },
  { id: 4, name: '个人独资企业' },
  { id: 5, name: '其他' },
]);
const options3 = ref([
  { id: 1, name: '正常使用' },
  { id: 0, name: '已停用' },
]);
// 列显隐信息
const columns = ref([
  { key: 1, label: `公司全称`, visible: true },
  { key: 1, label: `公司简称`, visible: true },
  { key: 2, label: `公司地址`, visible: true },
  { key: 3, label: `所属行业`, visible: true },
  { key: 4, label: `企业规模`, visible: true },
  { key: 5, label: `企业性质`, visible: true },
  { key: 6, label: `企业类型`, visible: true },
  { key: 7, label: `产品和业务`, visible: true },
]);
const data = reactive({
  form: {
    id: null,
    entName: null,//企业名称
    entShortName: null,//企业简称
    entAddr: null,//企业地址
    industry: null,//所属行业
    entSize: null,//企业规模
    entType: null,//企业性质
    entStyle: null,//企业类型
    mainProducts: null,//产品和业务
    state: 1,//企业管理状态
  },
  queryParams: {
    pageNo: 1,
    pageSize: 10,
    entName: null,//名称
    state: null,//企业管理状态
  },
  rules: {
    entName: [{ required: true, message: "企业名称不能为空", trigger: "blur" }],
    entShortName: [{ required: true, message: "企业简称不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

// 自定义校验函数
const validateTypeOrSpec = (rule, value, callback) => {
  if (!form.value.types && !form.value.specifications) {
    callback(new Error('型号和规格至少输入一项'));
  } else {
    callback();
  }
};
// 更新校验规则
rules.value.types = { validator: validateTypeOrSpec, trigger: 'blur' };
rules.value.specifications = { validator: validateTypeOrSpec, trigger: 'blur' };

/** 查询角色列表 */
function getList() {
  loading.value = true;
  proxy.cloudTv_API.GetEnterprisePageList(queryParams.value).then(response => {
    dataList.value = response.data.list;
    total.value = response.data.total;
    loading.value = false;
  });
}
// 输入时触发的防抖处理函数
let timer = null;
function handleInput() {
  clearTimeout(timer);
  timer = setTimeout(handleQuery, 300);
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNo = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除企业"' + row.entName + '"的数据项?').then(function () {
    proxy.cloudTv_API.DelStationPrise({
        id: row.id,
      }).then(response => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      });
  })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("system/role/export", {
    ...queryParams.value,
  }, `role_${new Date().getTime()}.xlsx`);
}


/** 重置新增的表单以及其他数据  */
function reset() {
  if (menuRef.value != undefined) {
    menuRef.value.setCheckedKeys([]);
  }
  form.value = {
    id: null,
    entName: null,//企业名称
    entShortName: null,//企业简称
    entAddr: null,//企业地址
    industry: null,//所属行业
    entSize: null,//企业规模
    entType: null,//企业性质
    entStyle: null,//企业类型
    mainProducts: null,//产品和业务
    state: 1,//企业管理状态
  };
}
/** 新增物料 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增";
}

/** 修改物料 */
function handleUpdate(row) {
  let newRow = JSON.parse(JSON.stringify(row));
  reset();
  Object.keys(form.value).forEach(key => {
    form.value[key] = newRow[key];
  });
  open.value = true;
  title.value = "修改";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["roleRef"].validate(valid => {
    if (valid) {
      if (form.value.id) {
        proxy.cloudTv_API.EditEnterPrise({
          ...form.value,
        }).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        proxy.cloudTv_API.AddEnterPrise({
          ...form.value,
        }).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
// 复制
async function copy(text) {
  console.log(text);
  try {
    await navigator.clipboard.writeText(text);
    proxy.$modal.msgSuccess('复制成功');
  } catch (err) {
    console.log(err);

  }
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

getList();
</script>

<style lang="scss" scoped>
.el-input-number {
  width: 190px;
}

.required-star {
  color: #F56C6C;
  margin-right: 4px;
}
</style>
